package com.dp.appi.controller;

import com.dp.appi.entity.UserEntity;
import com.dp.appi.exception.CommonException;
import com.dp.appi.service.ConsumerService;
import com.dp.appi.service.FavoriteService;
import com.dp.appi.util.ExceptionUtil;
import com.dp.appi.util.SYS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/** 收藏
 * Created by xiaoji on 2016/4/29.
 */
@Controller
@RequestMapping(value = "/favoriteControl")
public class FavoriteControl {
    private static Logger log = LoggerFactory.getLogger(FavoriteControl.class);
    @Autowired
    private FavoriteService favoriteService;
    @Autowired
    private ConsumerService consumerService;

    /**
     * 获取收藏列表
     * @param modelMap
     * @param param
     * @param s 开始序列
     * @param n 获取条数
     */
    @RequestMapping(value = "/collectList",method = RequestMethod.GET)
    public void collectList(ModelMap modelMap,@RequestParam Map<String,String> param,Integer s,Integer n) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            modelMap.addAttribute("collectList", favoriteService.collectList(param.get("name"),s,
                    n,String.valueOf(map.get("id"))));
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 添加用户收藏百科
     * @param modelMap
     * @param param
     * @param encyclopedia_id
     */
    @RequestMapping(value = "/addCollect",method = RequestMethod.POST)
    public void addCollect(ModelMap modelMap,@RequestParam Map<String,String> param,Long encyclopedia_id) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);

            //判断是否已经添加
            if(null!=favoriteService.getCollect(encyclopedia_id,String.valueOf(map.get("id")))){
                throw new CommonException("EXPO017");
            }
            modelMap.addAttribute("favorite_id",favoriteService.addCollect(encyclopedia_id,String.valueOf(map.get("id"))));
        }catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 删除收藏
     * @param modelMap
     * @param param
     * @param favorite_id 收藏id
     */
    @RequestMapping(value = "/delCollect",method = RequestMethod.POST)
    public void delCollect(ModelMap modelMap,@RequestParam Map<String,String> param,Long favorite_id) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            favoriteService.delCollect(favorite_id, String.valueOf(map.get("id")));
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }
}
